//
//  KKTabBarController.m
//  DoMi
//
//  Created by 康凯 on 16/10/27.
//  Copyright © 2016年 kk. All rights reserved.
//

#import "KKTabBarController.h"
#import "KKNavController.h"
#import "KKTabBar.h"

@interface KKTabBarController()<KKTabbarDelegate>

@property (nonatomic, strong) KKTabBar * kkTabBar;

@end

@implementation KKTabBarController

-(KKTabBar *)kkTabBar{
    if (!_kkTabBar) {
        _kkTabBar = [[KKTabBar alloc] initWithFrame:CGRectMake(0,0, screenWidth, 48)];
        _kkTabBar.delegate=self;
    }
    return _kkTabBar;
}

- (void)viewWillLayoutSubviews{
    CGRect tabFrame = self.tabBar.frame; //self.TabBar is IBOutlet of your TabBar
    tabFrame.size.height = 48;
    tabFrame.origin.y = self.view.frame.size.height - 48;
    self.tabBar.frame = tabFrame;
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    //加载所有视图控制器
    [self addViewControllers];
    
    //加载自定义tabbar
    [self.tabBar addSubview:self.kkTabBar];
    
    [[UITabBar appearance] setShadowImage:[UIImage new]];
    [[UITabBar appearance] setBackgroundImage:[[UIImage alloc]init]];
    
}

#pragma mark - 增加viewcontroller

- (void)addViewControllers {
    
    NSMutableArray * viewControlNames = [NSMutableArray arrayWithArray:@[@"KKStudyVC",@"KKCommunicationController",@"KKMyController"]];
    
    for (NSUInteger i = 0; i < viewControlNames.count; i ++) {
        
        NSString *controllerName = viewControlNames[i];
        
        UIViewController *vc = [[NSClassFromString(controllerName) alloc] init];
        
        KKNavController *nav = [[KKNavController alloc] initWithRootViewController:vc];
        
        [viewControlNames replaceObjectAtIndex:i withObject:nav];
    }
    
    self.viewControllers = viewControlNames;
    
}

#pragma mark - KKTabbarDelegate

- (void)tabbar:(KKTabBar *)tabbar clickIndex:(long)tag {
    self.selectedIndex=tag;
    
}

@end
